/**********************************************************
 * Copyright © 2008，北京福富软件技术股份有限公司
 * All Rights Reserved.
 *
 * 文件名称： DownloadStreamProvider.java
 * 摘    要：
 *
 * 初始版本：1.0.0
 * 原 作 者：RUN
 * 完成日期：2009-9-10
 *
 * 当前版本：$LastChangedRevision$
 * 作    者：$LastChangedBy$
 * 完成日期：$LastChnagedDate$
 ************************************************************/
package aip.ui.attachment.web.spring;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import aip.core.model.attachment.Attachment;
import aip.core.model.attachment.Photo;
import aip.ui.attachment.srvc.AttachmentService;

import com.googlecode.horncomb.web.spring.mvc.ServletResponeInputStreamProvider;
import com.googlecode.horncomb.web.spring.mvc.ServletResponeInputStreamSource;

/**
 * 获取附件
 * <p>
 * 负责人/principal: Daniel<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:
 * @author RUN
 * @version $Id: codetemplates.xml 2007 2009-05-08 08:36:14Z shiw $
 */
public class DownloadStreamProvider implements
        ServletResponeInputStreamProvider
{
    private AttachmentService attachmentService;
    private boolean showThumb;

    /**
     * {@inheritDoc}
     */
    public ServletResponeInputStreamSource getInputStreamSource(
            HttpServletRequest request) throws Exception
    {
        String id = request.getParameter("id");
        String type = request.getParameter("atchType");

        String fileName;
        InputStream is;
        if (showThumb)
        {
            Photo photo = attachmentService.getWithThumb(id, type);
            byte[] buf = ((byte[]) photo.getThumbnail());
            is = new ByteArrayInputStream(buf);
            fileName = photo.getName().concat("_t.jpg");
        }
        else
        {
            Attachment at = attachmentService.getWithContent(id, type);
            byte[] buf = ((byte[]) at.getContent());
            is = new ByteArrayInputStream(buf);
            fileName = at.getName().concat(at.getExtension());
        }
        return new ServletResponeInputStreamSource(fileName, is);
    }

    /**
     * @param attachmentService
     *        the attachmentService to set
     */
    public void setAttachmentService(AttachmentService attachmentService)
    {
        this.attachmentService = attachmentService;
    }

    /**
     * @param showThumb
     *        the showThumb to set
     */
    public void setShowThumb(boolean showThumb)
    {
        this.showThumb = showThumb;
    }
}
